﻿var hg_time = null;
var lgjz = "";
var ltplj = "";
var sj_time = null;
var jkm1;
var jkm2;
function loadImage(url, callback) {
    var img = new Image(); //创建一个Image对象，实现图片的预下载
    img.src = url;
    if (img.complete) { // 如果图片已经存在于浏览器缓存，直接调用回调函数
        //callback.call(img);
        return; // 直接返回，不用再处理onload事件
    }
    img.onload = function () { //图片下载完毕时异步调用callback函数。
        callback.call(img);//将回调函数的this替换为Image对象
    };
}; 
function imgLoaded(){
    scrollTo(0,document.body.scrollHeight);
} 
function kaishihuoqu() {
  var huidiao = document.getElementById("huidiao");
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "sz/hd.php?&r=" + Math.random().toString(), true);
  xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function() {
    var XMLHttpReq = xhr;
    if (XMLHttpReq.readyState == 4) {
      if (XMLHttpReq.status == 200) {
        var text = XMLHttpReq.responseText;
        strs = text.split("|Anters_fg|");
        if (strs.length >= 1) {
          var gjzzs = "关键字：" + strs[0];
          if (lgjz != strs[0]) {
            huidiao.innerHTML = gjzzs;
            lgjz = strs[0];
          }
          if (strs.length >= 2 && strs[1] != ltplj) {
            var lsHTML;
            if(strs.length >= 2){
                strs[1] = encodeURIComponent(strs[1]);
                lsHTML  = gjzzs + "<br /><br /><div class='imgbox'><a target='_blank' href='images?url=" + strs[1] + "'><img src='images?url=" + strs[1] + "'></img></a></div>";
            }
            huidiao.innerHTML = lsHTML;
            ltplj = strs[1];
            loadImage("images?url=" + strs[1],imgLoaded);
          }
        }
        if (hg_time) clearTimeout(hg_time);
        hg_time = setTimeout("kaishihuoqu();", 3500);
      }
    }
  };
  xhr.send();
  hg_time = setTimeout("kaishihuoqu();", 6000);
}
function kaishi(a) {
  if (a.innerHTML == "停止") {
    if (hg_time) clearTimeout(hg_time);
    a.innerHTML = "自动跟踪";
    return;
  }
  a.innerHTML = "停止";
  var huidiao = document.getElementById("huidiao");
  huidiao.style.border = "1px solid red";
  kaishihuoqu();
}
function huizhuye(a){
    if(sfzzy != 1){
        var zylb = document.getElementById('zylb');
        var gzfslb = document.getElementById('gzfslb');
        var szlb = document.getElementById('szlb');
        var gllb = document.getElementById('gllb');
        var currentState = history.state;
        sfzzy = 1;
        zylb.style.display = 'block';
        gzfslb.style.display = 'none';
        szlb.style.display = 'none';
        gllb.style.display = 'none';
        a.innerHTML = '002';
        if(currentState != 'zy')
            history.pushState('zy', 'title', "?sx=1&pos=zy");
    }else{
        if(sj_time == null){
            sj_time = setTimeout(function(){
                sj_time = null;
            },1000);
        }else{
            window.open('http://baiducount.com');
        }
    }
}
function open4()
{
    var zylb = document.getElementById('zylb');
    var gzfslb = document.getElementById('gzfslb');
    var fanhuianniu = document.getElementById('fanhuianniu');
    var currentState = history.state;
    sfzzy = 0;
    fanhuianniu.innerHTML = '返回';
    zylb.style.display = 'none';
    gzfslb.style.display = 'block';
    if(currentState != 'gzfslb')
        history.pushState('gzfslb', 'title', "?sx=1&pos=gzfslb");
    
}
function open3()
{
    var zylb = document.getElementById('zylb');
    var fanhuianniu = document.getElementById('fanhuianniu');
    var szlb = document.getElementById('szlb');
    var gllb = document.getElementById('gllb');
    var currentState = history.state;
    sfzzy = 0;
    fanhuianniu.innerHTML = '返回';
    zylb.style.display = 'none';
    szlb.style.display = 'block';
    gllb.style.display = 'none';
    if(currentState != 'szlb')
        history.pushState('szlb', 'title', "?sx=1&pos=szlb");
    
}
function open2()
{
    var zylb = document.getElementById('zylb');
    var szlb = document.getElementById('szlb');
    var gllb = document.getElementById('gllb');
    var fanhuianniu = document.getElementById('fanhuianniu');
    var currentState = history.state;
    sfzzy = 0;
    fanhuianniu.innerHTML = '返回';
    zylb.style.display = 'none';
    szlb.style.display = 'none';
    gllb.style.display = 'block';
    if(currentState != 'gllb')
        history.pushState('gllb', 'title', "?sx=1&pos=gllb");
    
}
if (history.pushState) {
    history.replaceState('zy', "title", "?sx=1&pos=zy");
	window.addEventListener("popstate", function() {
        var currentState = history.state;
		switch(currentState){
		  case 'gllb':
            open2();
            break;
		  case 'szlb':
            open3();
            break;
		  case 'zy':
            huizhuye(document.getElementById('fanhuianniu'));
            break;
		  case 'gzfslb':
            open4();
            break;
		}
	});
}
$(document).ready(function(){
    $.get("sz/get/getGjcpm.php?pos=002", function(result){
        $("#tjgjc").html('推荐关键词：'+result);
    });
    jkm1 = document.getElementById('jkm1');
    jkm2 = document.getElementById('jkm2');
    setInterval(function(){
        $.get("002.php?sx=1&xq=1", function(result){
            if(result == '-1'){
                jkm1.innerHTML = '未连接';
                if(jkm2)jkm2.innerHTML = '未连接';
            }
            
        });
    },4 * 60 * 1000);
});
function getJkm(){
    jkm1.innerHTML = '<img src="images/loading.gif" style="width:33px;hieght:33px;" />';
    if(jkm2)
        jkm2.innerHTML = '<img src="images/loading.gif" style="width:33px;hieght:33px;" />';
    $.get("002.php?get=1",function(data,status){
        if(data.length <= 20){
            var datas = data.split('|');
            if(datas.length == 2){
                jkm1.innerHTML = '&nbsp;'+datas[0]+'&nbsp;';
                if(jkm2)jkm2.innerHTML = '&nbsp;'+datas[1]+'&nbsp;';
            }
        }
    });
}
function long_start(){
    var jkm = prompt("输入远程对接码：","");
    if(jkm != null && jkm != '' && jkm >= 0 && jkm <= 99)
        window.location.href='tybgz.php?dizeng=d'+jkm;
}